<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>授权成功</title>
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
</head>
<style>
  .root {
    margin-top: 70px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .title {
    margin-top: 40px;
    font-size: 18px;
    color: #000;
    font-weight: 600;
  }

  .subtitle {
    margin-top: 20px;
    font-size: 14px;
    color: #999;
  }

  #btn-back {
    margin-top: 60px;
    color: #fff;
    background-image: linear-gradient(90deg, #56bdfc, #b99cf9);
    width: 220px;
    height: 44px;
    line-height: 44px;
    font-size: 18px;
    border-radius: 6px;
    border: none;
    text-align: center;
    text-decoration: none;
  }

  a:active {
    opacity: 0.6;
  }
</style>

<body>
  <div class="root">
    <img width="120" height="120"
      src="" />
    <text class="title">授权成功</text>
    <text class="subtitle">已验证支付宝实名信息，请点击返回APP查看结果</text>
    <a onclick="backApp()" id="btn-back">返回APP</a>
  </div>

  <script type="text/javascript">

    // function toChineseStr(unicodeStr) {
    //   if (unicodeStr == '') {
    //     return 'Please input hexadecimal Unicode';
    //   }
    //   unicodeStr = unicodeStr.split('\\u').filter(Boolean);
    //   let chineseStr = '';
    //   for (let i = 0, iLength = unicodeStr.length; i < iLength; i++) {
    //     chineseStr += String.fromCharCode(parseInt(unicodeStr[i], 16));
    //   }
    //   return chineseStr;
    // }

    // function toUnicode(chineseStr) {
    //   if (chineseStr == '') {
    //     return 'Please input Chinese Characters';
    //   }
    //   let unicodeStr = '';
    //   for (let i = 0, iLength = chineseStr.length; i < iLength; i++) {
    //     unicodeStr += '\\u' + chineseStr.charCodeAt(i).toString(16);
    //   }
    //   return unicodeStr;
    // }

    const fromEntries = (arrArg) => {
      // Map
      if (Object.prototype.toString.call(arrArg) === '[object Map]') {
        const resMap = {};
        for (const key of arrArg.keys()) {
          resMap[key] = arrArg.get(key);
        }
        return resMap;
      }
      // array
      if (Array.isArray(arrArg)) {
        const resArr = {}
        arrArg.map(([key, value]) => {
          resArr[key] = value
        })
        return resArr
      }
      throw 'Uncaught TypeError: argument is not iterable';
    }

    const transformQueryString = params => {
      if (typeof params === "string") {
        const queryStrings = params.match(/[^?&]+=[^?&]+/g);

        return queryStrings ? fromEntries(
          queryStrings.map(item => item.split(/^([^=]*)=*/).filter(Boolean))
        ) : {};
      }
      if (typeof params === "object") {
        return Object.keys(params).filter(key => params[key]).map(key => `${key}=${params[key]}`).join("&");
      }
      alert("Parameter error");
    }

    function backApp() {
      const a = document.getElementById("btn-back");
      a.href = "uni://auth";
      a.click();
    }

    window.onload = () => {
      const ua = navigator.userAgent;
      if (/Alipay/i.test(ua)) {
        fetch("/api/authResult2", {
          method: "POST",
          headers: {
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(Object.assign({ type: 2 }, transformQueryString(location.search)))
        }).then(res => res.json())
          .then(res => { alert(res.msg); })
          .catch((err) => { alert(err); })
          .finally(() => {
            backApp();
            if (!!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
              const img = document.createElement("img");
              img.src = "/static/server/img/mask.png";
              img.style = "width: 100%; height: 100%; position: fixed; top: 0; right: 0; bottom: 0; left: 0;"
              document.body.appendChild(img);
            }
          });
        return;
      }
      backApp();
    }
  </script>
</body>

</html>